require "Common/define"
require "Logic/Config"
require "Common/functions"

PetPartItem = {};
HelperFunc.SetUIEnv(PetPartItem)
local this = PetPartItem;

this.img_Quality = nil;
this.img_Frame = nil
this.img_Icon = nil;
this.stateCtrl = nil;
this.txt_Level = nil;
this.txt_refine = nil;
this.txt_num = nil;
this.maskCtrl = nil;
this.dialogCtrl = nil;
this.cornerMark = nil;

function PetPartItem.Init()
    local name = getUIName(PanelNames.PetPartItem);
    panelMgr:ShowUI(name, this.onCreate, Config.useLocalUI);
end

function PetPartItem.onCreate(obj)
    obj:SetActive(false);
end
---@param obj GComponent
function PetPartItem.initData(obj, id, func,type)
        img_Quality = obj:GetChild("quality");
        img_Frame = obj:GetChild("frame")
        img_Icon = obj:GetChild("icon");
        this.name = obj:GetChild("name");
        stateController = obj:GetController("petbujian");
    if id == 0 then
        obj.onClick:Clear();
        stateController.selectedIndex = 1;
        img_Icon.url = "ui://PetAtlas/" .. "empty";
        img_Quality.url = EnumConst.AtlasPath.ICONAtlas .. Config.IconQuality[6];
        img_Frame.url = EnumConst.AtlasPath.ICONAtlas .. Config.IconFrame[6];
        if func then
            local cfg = {};
            cfg.f_ChromosomeId = type;
            obj.onClick:Clear();
            obj.onClick:Add(function()
                    func(cfg,obj)
            end);
        end
    else
        stateController.selectedIndex = 0;
        local cfg = G.dataTable["t_elvesgene"][tostring(id)];

        if cfg.f_MutationLevel == 2 then
            --突变
            img_Quality.url = EnumConst.AtlasPath.ICONAtlas .. Config.IconQuality[10];
            img_Frame.url = EnumConst.AtlasPath.ICONAtlas .. Config.IconFrame[10];
        else
            img_Quality.url = EnumConst.AtlasPath.ICONAtlas .. Config.IconQuality[6];
            img_Frame.url = EnumConst.AtlasPath.ICONAtlas .. Config.IconFrame[6];
        end
        --试穿
        if cfg.f_ResId ~= "empty" then
            obj.onClick:Clear();
            obj.onClick:Add(function()
                 if func then
                    func(cfg)
                else
                    showUI(PanelNames.PetCommonPop, cfg,1)
                end
            end);
        else
            obj.onClick:Clear();
            if func then
                local cfg = {};
                cfg.f_ChromosomeId = type;
                obj.onClick:Add(function()
                    func(cfg)
                end);
            end
            stateController.selectedIndex = 1;

        end
        img_Icon.url = "ui://PetAtlas/" .. cfg.f_ResId;
    end

end

function PetPartItem.DisableClick(obj)
    obj.onClick:Clear();
end

return PetPartItem;